package com.cn.crmsystem.service.approveService.impl;
import com.cn.crmsystem.mapper.approveMapper.TravelMapper;
import com.cn.crmsystem.pojo.*;
import com.cn.crmsystem.service.approveService.TravelService;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class TravelServiceImpl implements TravelService{
	@Autowired TravelMapper travelMapper;
	//查询审核人
	@Override
	public ResultJson selectLeader(Integer userid) {
		ResultJson resultJson=new ResultJson();
		String code;
		String msg;
		Integer c=travelMapper.selectleaderId(userid);
		User  user= new User();
		user=travelMapper.selectleader(c);
	   if(c>0&&user!=null){
	   	code="200";
		   resultJson.getSuccess(code,user);
	   }else{
	   	msg="失败";
		   resultJson.getFail(msg);
	   }
		return resultJson;
	}
	//插入
	@Override
	public ResultJson TravelInsert(Travel travel, Integer leader) {
		ResultJson resultJson=new ResultJson();
		String code;
		String msg;
		try{
			Integer c=travelMapper.InsertTravel(travel);
			Integer travelid=travel.getTravel_Id();
			Integer type=1;
			if(c>0) {
				Integer d=travelMapper.InsertApprove(leader,travelid,type);
				if (d>0){
					code="200";
					msg = "成功";
					resultJson.getSuccess(msg,code);
				}else{
					msg = "失败";
					resultJson.getSuccess(msg);
				}

			}else{
				msg = "失败";
				resultJson.getSuccess(msg);
			}
		}catch (Exception e){
			msg=e.getMessage();
			resultJson.getFail(msg);
		}
		return resultJson;
	}
	//查询出差列表
	@Override
	public ResultJson SelectMyTravel(Integer userid,Integer method) {
		ResultJson resultJson=new ResultJson();
		String msg;
		List<Approve> list=new ArrayList<>();
		if(method==0){
			list=travelMapper.selectMYTravel(userid);
			if(list!=null) {
				resultJson.getSuccess("成功",list);
			}else{
				resultJson.getSuccess("失败");
			}
		}else{
			list=travelMapper.selectApproveTravel(userid);
			if(list!=null) {
				resultJson.getSuccess("成功",list);
			}else{
				resultJson.getSuccess("失败");
			}
		}
		return resultJson;
	}
	//删除
	@Override
	public ResultJson DeleteTravel(Integer appid) {
		ResultJson resultJson=new ResultJson();
		String code;
		String msg;
		if(travelMapper.DeleteTravel(appid)>0) {
			int travelid=travelMapper.travelid(appid);
			if(travelMapper.DeleteApprove(travelid)>0){
				code="200";
				msg = "成功";
				resultJson.getSuccess(msg,code);
			}else{
				msg="失败";
				resultJson.getSuccess(msg);
			}

			code="200";
			msg = "成功";
			resultJson.getSuccess(msg,code);
		}else{
			code="200";
			msg = "没数据";
			resultJson.getSuccess(msg,code);
		}
		return resultJson;
	}
	//更新
	@Override
	public ResultJson UpdateTravel(Integer appid,Integer state) {
		ResultJson resultJson=new ResultJson();
		String code;
		String msg;
		if(travelMapper.UpdateTravel( appid,state)>0) {
			code="200";
			msg = "成功";
			resultJson.getSuccess(msg,code);
		}else{
		msg="失败";
			resultJson.getSuccess(msg);
		}
		return resultJson;
	}
	//条件查询
	@Override
	public ResultJson SelectConditionTravel(Integer userid, String method, String state) {
		ResultJson resultJson=new ResultJson();
		Map<String,Object> map=new HashMap<>();
		Approve ap=new Approve();
		List<Approve> condtion=new ArrayList<>();
		if(method=="0"){
			condtion=travelMapper.selectMyCondition(userid,state);
			if(condtion!=null) {
				map.put("我发出出差列表",condtion);
				resultJson.getSuccess("成功",map);
			}else{
				resultJson.getSuccess("失败");
			}
		}else{
			condtion=travelMapper.selectApproveCondition(userid,state);
			if(condtion!=null) {
				map.put("我审核出差列表",condtion);
				resultJson.getSuccess("成功",map);
			}else{
				resultJson.getSuccess("失败");
			}
		}
		return resultJson;
	}
}
